<template>
  <el-dialog v-if="load" title="批量设置IOSS税号" v-model="visible" align-center width="600" @close="handleClose" :close-on-click-modal="false">
    <el-form class="py-4" :rules="rules" ref="ruleFormRef" :model="data" label-position="top" >
      <el-form-item label="IOSS税号" prop="sander_tax_id">
        <el-input placeholder="请输入" v-model="data.sander_tax_id" class="w-[400px]" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, toRefs } from 'vue';
import { batchUpdate } from '../api'
import { ElMessage } from 'element-plus'
const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const ruleFormRef = ref()
const options = [{
  label: '是',
  value: '1'
}, {
  label: '否',
  value: '0'
},]
const data = reactive({
  sander_tax_id: '',
  ids: []
})
const rules = reactive({
  sander_tax_id: [
    {
      required: true,
      message: '税号不能为空',
      trigger: 'blur',
    },
  ],
})
const { sander_tax_id, ids } = toRefs(data)
const emit = defineEmits(['close', 'confirm', 'query'])

// 打开弹窗
const open = async (arr) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  loadings = reactive({})
  ids.value = arr
  visible.value = true
}
// 关闭弹窗
const close = () => {
  sander_tax_id.value = ''
  visible.value = false
  ruleFormRef.value.resetFields()
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = async () => {
  let { validate } = ruleFormRef.value
  validate((valid) => {
    if (valid) {
      loadings.submit = true
      batchUpdate({ ids: ids.value, orderSource: { sander_tax_id: sander_tax_id.value } }).then(res => {
        ElMessage.success('操作成功')
        emit('query')

        close()
      }).finally(() => {
        loadings.submit = false
      })
    }
  })
}

defineExpose({ open, close, loadings })
</script>
<style lang="scss" scoped>
:deep(.el-form-item) {
  width: 400px;
  margin: 0 auto;
}
</style>